﻿
using System;
using System.Collections;

namespace BusinessUnitOrgChart
{

    public enum SortDirection
    {
        Asc,
        Desc
    }

    public class Comparer : IComparer
    {

        private SortDirection m_direction = SortDirection.Asc;

        public Comparer() : base() { }

        public Comparer(SortDirection direction)
        {
            this.m_direction = direction;
        }

        int IComparer.Compare(object x, object y)
        {

            BusinessUnit idX = (BusinessUnit)x;
            BusinessUnit idY = (BusinessUnit)y;

            if (idX == null && idY == null)
            {
                return 0;
            }
            else if (idX == null && idY != null)
            {
                return (this.m_direction == SortDirection.Asc) ? -1 : 1;
            }
            else if (idX != null && idY == null)
            {
                return (this.m_direction == SortDirection.Asc) ? 1 : -1;
            }
            else
            {
                return (this.m_direction == SortDirection.Asc) ?
                    idX.level.CompareTo(idY.level) :
                    idY.level.CompareTo(idX.level);
            }
        }
    }
}